package org.maxgroundstation.groundstation.view.map;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.util.Iterator;
import java.util.List;

import org.maxgroundstation.groundstation.GroundStation;
import org.maxgroundstation.groundstation.GroundStationEvent;
import org.maxgroundstation.groundstation.GroundStationListener;
import org.maxgroundstation.world.landmark.Landmark;

/**
 * A map overlay for displaying landmarks.
 * @author Scott Davis
 * 
 * Copyright (C) 2012  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class LandmarksMapOverlay implements MapOverlay {

    // The overlay ID.
    public static final String OVERLAY_ID = "Landmarks";
    private static final Color FG_COLOR = new Color(255, 127, 0);
    private static final Color BG_COLOR = new Color(0, 0, 0, 127);
    private static final Stroke FG_STROKE = new BasicStroke(2F);
    private static final Stroke BG_STROKE = new BasicStroke(4F);
    private static final int DISPLAY_RANGE = 5000;
    
    // Data members
    private List<Landmark> landmarks;
    
    /**
     * Constructor
     * @param gs the ground station.
     */
    public LandmarksMapOverlay(GroundStation gs) {
        
        // Set landmarks when world model is set for the ground station.
        gs.addGroundStationListener(new GroundStationListener() {

            @Override
            public void groundStationUpdate(GroundStationEvent event) {
                if (GroundStationEvent.WORLD_MODEL_SET.equals(event.getType())) {
                    GroundStation gs = (GroundStation) event.getSource();
                    setLandmarks(gs.getWorldModel().getLandmarks());
                }
            }
        });
    }
    
    /**
     * Sets the landmarks.
     * @param landmarks list of landmarks.
     */
    private void setLandmarks(List<Landmark> landmarks) {
        this.landmarks = landmarks;
    }
    
    @Override
    public void drawMapOverlay(String baseMapStyle, Graphics2D g2d, GpsLocation currentLoc, int width, int height,
            float zoom) {
        
        int xCenter = width / 2;
        int yCenter = height / 2;
        
        Stroke saveStroke = g2d.getStroke();
        
        // Draw landmarks.
        if ((currentLoc != null) && (landmarks != null)) {
            
            Iterator<Landmark> i = landmarks.iterator();
            while (i.hasNext()) {
                Landmark landmark = i.next();
                GpsLocation landmarkGpsLoc = new GpsLocation(landmark.getLatitude(), landmark.getLongitude());
                int landmarkLocX = xCenter - (int) (MapUtil.getXDiff(landmarkGpsLoc, currentLoc) / zoom);
                int landmarkLocY = (int) (MapUtil.getYDiff(landmarkGpsLoc, currentLoc) / zoom) + yCenter;
                
                int xDiff = Math.abs(xCenter - landmarkLocX);
                int yDiff = Math.abs(yCenter - landmarkLocY);
                if ((xDiff < DISPLAY_RANGE) && (yDiff < DISPLAY_RANGE)) {
                
                    // Draw each point of interest as red triangle.
                    int[] triangleX = {landmarkLocX - 3, landmarkLocX, landmarkLocX + 3};
                    int[] triangleY = {landmarkLocY + 3, landmarkLocY - 3, landmarkLocY + 3};
                    
                    g2d.setColor(BG_COLOR);
                    g2d.setStroke(BG_STROKE);
                    g2d.drawPolygon(triangleX, triangleY, 3);
                    
                    g2d.setColor(FG_COLOR);
                    g2d.setStroke(FG_STROKE);
                    g2d.drawPolygon(triangleX, triangleY, 3);
                }
            }
        }
        
        g2d.setStroke(saveStroke);
    }

    @Override
    public boolean isDraggable() {
        return true;
    }
}